package com.coldchain.admin.user.api;


import com.coldchain.admin.user.service.TokenService;
import com.coldchain.admin.user.service.impl.UserService;
import com.coldchain.authclient.annotation.CheckClientToken;
import com.coldchain.authclient.annotation.CheckUserToken;
import com.coldchain.common.auth.AuthUser;
import com.coldchain.common.exception.base.BusinessException;
import com.coldchain.common.msg.ObjectRestResponse;
import com.fangliai.core.entitiy.SystemModelEnum;
import com.fangliai.core.exception.BaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author fangliai
 * @since 2018-08-30
 */
@RestController
@RequestMapping("/user")
@ApiIgnore
public class UserController {
    private static final Logger log= LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;
    @Autowired
    private TokenService tokenService;

    /**
     * 用户密码登录认证
     * @param scope
     * @param loginName
     * @param password
     * @return
     */
    @PostMapping(value = "/validate/pwd")
    public ObjectRestResponse<AuthUser> passwordValidate(String scope, String loginName, String password)throws BaseException {
          SystemModelEnum scopeEnum= SystemModelEnum.valueOf(scope.toUpperCase());
          if(scopeEnum==null){
               throw  new BusinessException("参数错误");
          }
          return  new ObjectRestResponse<>().data(tokenService.passwordLogin(scopeEnum,loginName,password)) ;
      }

     @GetMapping(value = "/info/{userId}")
     public  ObjectRestResponse<Map<String,Object>> getUserInfo(
                    @PathVariable(value = "userId",required=false) String userId){
            return null;
      }


      @CheckUserToken
      @CheckClientToken
      @PostMapping(value = "/modifyPassword")
      public  Object modifyPassword(String oldpwd,String newspwd){
          userService.modifyPassword(oldpwd,newspwd);
          return new ObjectRestResponse().succeed("修改成功");
      }
}
